package top.xxyu.controller;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import top.xxyu.annotation.Log;
import top.xxyu.core.Msg;
import top.xxyu.dao.UserMailDao;
import top.xxyu.entity.UserMail;
import top.xxyu.util.BeanUtils;
import top.xxyu.util.UUIDUtils;

import java.util.Objects;

@RestController
@RequestMapping(value = "userMail")
public class UserMailController {


    @Autowired
    private UserMailDao userMailDao;


    @ApiOperation(value = "/", httpMethod = "POST", notes = "添加用户邮箱配置")
    @Log(desc = "添加用户邮箱配置")
    @PostMapping
    public Msg add(@RequestBody UserMail userMail){

        if (Objects.isNull(userMail)) {
            return Msg.error("添加有用户邮箱配置失败");
        }
        if(StringUtils.isBlank(userMail.getId())) {
            userMail.setId(UUIDUtils.getID());
        }
        userMailDao.save(userMail);
        return Msg.success("添加用户邮箱配置成功");

    }

    @ApiOperation(value = "/", httpMethod = "PUT", notes = "更新用户邮箱配置")
    @Log(desc = "更新用户邮箱配置")
    @PutMapping
    public Msg update(@RequestBody UserMail userMail){

        if (Objects.isNull(userMail)) {
            return Msg.error("添加有用户邮箱配置失败");
        }
        UserMail usermailDao = userMailDao.getOne(userMail.getId());
        BeanUtils.copyPropertiesIgnoreNull(userMail, usermailDao);
        userMailDao.save(usermailDao);
        return Msg.success("添加用户邮箱配置成功");

    }

    @ApiOperation(value = "/{page}/{size}", httpMethod = "GET", notes = "获取用户邮箱配置列表")
    @Log(desc = "获取用户邮箱配置列表")
    @GetMapping(value = "/{page}/{size}")
    public Msg getAll(@PathVariable int page, @PathVariable int size){
        return Msg.success("获取用户邮箱配置列表",userMailDao.findAll(PageRequest.of(page - 1, size)));

    }

    @ApiOperation(value = "/{id}", httpMethod = "DELETE", notes = "删除用户邮箱配置")
    @Log(desc = "删除用户邮箱配置")
    @DeleteMapping
    public Msg delete(@PathVariable String id){
        userMailDao.deleteById(id);
        return Msg.success("删除用户邮箱配置成功");

    }

}
